package com.bifang.module.file.common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

@Component
public class FastDFSClient {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired private FastFileStorageClient fastFileStorageClient;

    /**
     * 文件上传 最后返回fastDFS中的文件名称;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc
     *
     * @param fastFile 文件信息
     * @return fastDfs路径
     */
    public StorePath uploadFile(FastFile fastFile) {
        StorePath storePath = fastFileStorageClient.uploadFile(fastFile);
        logger.info(
                storePath.getGroup()
                        + "=="
                        + storePath.getPath()
                        + "======"
                        + storePath.getFullPath());
        return storePath;
    }

    /** 获取文件元数据 */
    public Set<MetaData> getMetadata(String groupName, String path) {
        Set<MetaData> metaData = fastFileStorageClient.getMetadata(groupName, path);
        return metaData;
    }

    /** 获取文件信息 */
    public FileInfo queryFileInfo(String groupName, String path) {
        FileInfo fileInfo = fastFileStorageClient.queryFileInfo(groupName, path);
        return fileInfo;
    }

    /**
     * 下载文件
     *
     * @param fileUrl 文件URL
     * @return 文件字节
     * @throws IOException
     */
    public byte[] downloadFile(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        return downloadFile(group, path);
    }

    public byte[] downloadFile(String groupName, String path) {
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = fastFileStorageClient.downloadFile(groupName, path, downloadByteArray);
        return bytes;
    }

    public File download(String groupName, String path, File file) {
        fastFileStorageClient.downloadFile(
                groupName,
                path,
                new DownloadCallback<File>() {
                    @Override
                    public File recv(InputStream ins) {
                        FileUtil.writeFromStream(ins, file);
                        return file;
                    }
                });
        return file;
    }

    public File download(String groupName, String path, String localPath) {
        File file =
                fastFileStorageClient.downloadFile(
                        groupName,
                        path,
                        new DownloadCallback<File>() {
                            @Override
                            public File recv(InputStream ins) {
                                File file = FileUtil.writeFromStream(ins, localPath);
                                return file;
                            }
                        });
        return file;
    }

    public InputStream download(String groupName, String path) {
        InputStream ins =
                fastFileStorageClient.downloadFile(
                        groupName,
                        path,
                        new DownloadCallback<InputStream>() {
                            @Override
                            public InputStream recv(InputStream ins) throws IOException {
                                // 将此ins返回给上面的ins
                                return ins;
                            }
                        });
        return ins;
    }
    public void download(String fileName,String groupName, String path,HttpServletResponse response) {
        fastFileStorageClient.downloadFile(
                        groupName,
                        path,
                        new DownloadCallback<InputStream>() {
                            @Override
                            public InputStream recv(InputStream ins) throws IOException {
                                final String contentType =
                                        ObjectUtil.defaultIfNull(
                                                FileUtil.getMimeType(fileName), "application/octet-stream");
                                ServletUtil.write(response, ins, contentType, fileName);
                                return ins;
                            }
                        });
    }
    /** 删除文件 */
    public void deleteFile(String groupName, String path) {
        fastFileStorageClient.deleteFile(groupName, path);
    }

    /** 删除文件 */
    public void deleteFile(String fileUrl) {
        fastFileStorageClient.deleteFile(fileUrl);
    }
}
